var app = getApp();

Page({
  data: {
    isLoading: false,
    tipwords: '',
    isQuery: false,
    queryTime: '',
    queryAddress: ''
  },
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    });
    this.getTipsWord(); // 获取提示语
    this.getUserApoitMsg(); // 获取用户预约信息
  },
  // 获取提示语
  getTipsWord: function () {
    var that = this;
    wx.request({
      url: app.globalData.apiBaseUrl + '/homePageSetting/getSystemSetting',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        if (res.data.code == 200) {
          that.setData({
            tipwords: res.data.data.infoPageNote
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
    setTimeout(function () {
      wx.hideToast()
    }, 3000)
  },
  // 获取用户预约信息
  getUserApoitMsg: function() {
    var that = this;
    var userId = wx.getStorageSync('openid');
    wx.request({
      url: app.globalData.apiBaseUrl + '/studentOrder/getUserOrderDetail',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      data: {
        userId: userId
      },
      dataType: 'json',
      success: function (res) {
        if (res.data.code == 200) {
          var lastData = res.data.data;
          if (lastData && lastData.length > 0) {
            lastData = lastData[lastData.length - 1];
            var appointTime = lastData.createTime.split('-');
            appointTime = appointTime[0] + '年' + appointTime[1] + '月' + appointTime[2] + '日';
            var morningOrAfterText = lastData.morningOrAfter == 0 ? '上午' : '下午'
            that.setData({
              isQuery: true,
              queryAddress: lastData.province + lastData.ciyt + lastData.area + (lastData.detailInfoAddress || ''),
              queryTime: appointTime + ' ' + morningOrAfterText
            })
            that.setData({
              isLoading: true
            });
          } else {
            that.setData({
              isQuery: false,
              isLoading: true
            })
          }
        } else {
          that.setData({
            isQuery: false,
            isLoading: true
          })
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      },
      fail: function(err) {
        that.setData({
          isQuery: false,
          isLoading: true
        })
        wx.showToast({
          title: err,
          icon: 'none'
        })
      }
    });
    wx.hideLoading();
    setTimeout(function () {
      wx.hideToast()
    }, 3000)
  },
  goAppointment: function() {
    wx.navigateTo({
      url: "../appointment/appointment"
    })
  }
})